/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vzone.market;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import vzone.tools.Date;
import java.util.Iterator;
import java.util.UUID;
import vzone.bank.Bank;
import vzone.community.Community;
import vzone.tools.SqlHelper;
import vzone.structs.Billing;
import vzone.structs.Company;
import vzone.structs.Goods;
import vzone.structs.OperatorCode;

/**
 *
 * @author Kingpro
 */
public class Shop implements IShop
{

    private Company company;
    private static final String SELECTCOMPANY = "SELECT * FROM [m_shop] WHERE [shopId]=?";
    private static final String SELECTGOODS = "SELECT * FROM [m_goods] WHERE [companyId]=?;";
    private static final String EXCHANGERECORD = "INSERT INTO [m_shopExchange] VALUES(?,?,?,?,?,?,?,?,?);";


    public Shop(UUID companyID) throws Exception
    {
	company = new Company();

	ArrayList companysAl = SqlHelper.ExecuteReader(SqlHelper.getConnect(), SELECTCOMPANY, new Object[]
		{
		    companyID.toString()
		});
	Object[] resultObjects = (Object[]) companysAl.iterator().next();
	company.IdUuid = companyID;
	company.Owner = Community.getCommunity().GetPeopleInfo(UUID.fromString((String) resultObjects[2]));
	company.NameS = (String) resultObjects[1];
	company.StartD =Long.parseLong(resultObjects[3].toString());
	company.DescriptionS = (String) resultObjects[4];
	company.pwd = (String) resultObjects[5];
    }

    @Override
    public OperatorCode ListAll(ArrayList<Goods> lists)
    {
	try
	{
	    //执行SQL
	    ArrayList resultAl = SqlHelper.ExecuteReader(SqlHelper.getConnect(), SELECTGOODS, new Object[]{company.IdUuid.toString()});

	    for(int  i=0; i<resultAl.size(); i++)
	    {
		Object[] resultOs = (Object[]) resultAl.get(i);
		Goods good = new Goods();
		good.CompanyIdUuid = company.IdUuid;
		good.IdUuid = UUID.fromString(resultOs[0].toString());
		good.NameS = (String) resultOs[1];
		good.SaleMoneyD = Double.parseDouble(resultOs[4].toString());
		good.CostsD = Double.parseDouble(resultOs[6].toString());
		good.CountI = Integer.parseInt(resultOs[5].toString());
		lists.add(good);
	    }
	    return OperatorCode.SUCCESS;
	}
	catch (Exception ex)
	{
	    return OperatorCode.OTHER_ERROR;
	}

    }

    @Override
    public OperatorCode BuyObjects(ArrayList<Goods> objsL, UUID BillingID)
    {
	Iterator<Goods> goods = objsL.iterator();
	double sumupD = 0;
	while (goods.hasNext())
	{
	    Goods good = goods.next();
	    sumupD += good.SaleMoneyD * good.CountI;
	}
	Billing billing = new Billing();
	billing.BillingIDUuid=BillingID;
	billing.ToAccount=company.accountIdUuid;
	OperatorCode code = Bank.getBank().CheckInBilling(billing, company.pwd);
	if (code == OperatorCode.SUCCESS)
	{
	    if (billing.Balance == sumupD)
	    {
		//执行记录操作
		Iterator<Goods> goodst = objsL.iterator();
		while (goodst.hasNext())
		{
		    Goods good = goodst.next();
		    try
		    {
			SqlHelper.ExecuteNonQuery(SqlHelper.getConnect(), EXCHANGERECORD, new Object[]
				{
				    UUID.randomUUID().toString(),
				    company.IdUuid.toString(),
				    billing.FromAccount.toString(),
				    good.IdUuid.toString(),
				    good.NameS,
				    good.SaleMoneyD,
				    good.CountI,
				    good.SaleMoneyD * good.CountI,
				    (new Date()).getTime()
				});
		    }
		    catch (Exception ex)
		    {
		    }
		}
		return code;
	    }
	    else
	    {
		UUID tmpUuid=billing.FromAccount;
		billing.FromAccount=billing.ToAccount;
		billing.ToAccount=tmpUuid;
		Bank.getBank().Transfer (billing, company.pwd);
		return OperatorCode.NOFITBILLING;
	    }
	}
	else
	{
	    return code;
	}
    }

    @Override
    public Company GetBusinessInfo()
    {
	return company;
    }
}
